{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "14007fc2-be2a-497a-940d-4330bf2b46d3", "metadata": {}, "outputs": [], "source": [ "# Imports\n", "\n", "from pgmpy.models import DynamicBayesianNetwork as DBN\n", "from pgmpy.factors.discrete import TabularCPD" ] }, { "cell_type": "code", "execution_count": 2, "id": "9d5ea2e4-a280-4622-9045-13bc2b4cce2a", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "06cfdf96f46c42c2a833c5021bd71a43", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/40 [00:00, , , , , , , ]\n" ] } ], "source": [ "# Fitting model parameters to a defined network structure.\n", "\n", "# Define the network structure for which to learn the model parameters. Here, we have assumeed the same model\n", "# structure that we simulated the data from\n", "dbn = DBN()\n", "dbn.add_edges_from([\n", " (('W', 0), ('O', 0)), # Weather influences ground observation\n", " (('T', 0), ('H', 0)), # Temperature influences humidity\n", " (('W', 0), ('H', 0)) # Weather influences humidity\n", "])\n", "dbn.add_edges_from([\n", " (('W', 0), ('W', 1)), # Weather transition\n", " (('T', 0), ('T', 1)), # Temperature transition\n", " (('W', 0), ('T', 1)) # Weather influences future temperature\n", "])\n", "\n", "# Fit the model using simulated samples\n", "dbn.fit(samples)\n", "print(dbn.cpds)" ] }, { "cell_type": "code", "execution_count": 4, "id": "bfd51960-dda5-40ea-a0e6-499e490c699e", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/tmp/ipykernel_1736345/1916813029.py:18: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n", " df_long = pd.concat([df_long, samples_t])\n", "/tmp/ipykernel_1736345/1916813029.py:18: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n", " df_long = pd.concat([df_long, samples_t])\n", "/tmp/ipykernel_1736345/1916813029.py:18: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n", " df_long = pd.concat([df_long, samples_t])\n", "/tmp/ipykernel_1736345/1916813029.py:18: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n", " df_long = pd.concat([df_long, samples_t])\n", "/tmp/ipykernel_1736345/1916813029.py:18: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n", " df_long = pd.concat([df_long, samples_t])\n", "/tmp/ipykernel_1736345/1916813029.py:18: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n", " df_long = pd.concat([df_long, samples_t])\n", "/tmp/ipykernel_1736345/1916813029.py:18: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n", " df_long = pd.concat([df_long, samples_t])\n", "/tmp/ipykernel_1736345/1916813029.py:18: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n", " df_long = pd.concat([df_long, samples_t])\n", "INFO:pgmpy: Datatype (N=numerical, C=Categorical Unordered, O=Categorical Ordered) inferred from data: \n", " {'T0': 'C', 'W0': 'C', 'H0': 'C', 'O0': 'C', 'T1': 'C', 'W1': 'C', 'H1': 'C', 'O1': 'C'}\n", "INFO:pgmpy: Datatype (N=numerical, C=Categorical Unordered, O=Categorical Ordered) inferred from data: \n", " {'T0': 'C', 'W0': 'C', 'H0': 'C', 'O0': 'C', 'T1': 'C', 'W1': 'C', 'H1': 'C', 'O1': 'C'}\n", "INFO:pgmpy: Datatype (N=numerical, C=Categorical Unordered, O=Categorical Ordered) inferred from data: \n", " {'T0': 'C', 'W0': 'C', 'H0': 'C', 'O0': 'C', 'T1': 'C', 'W1': 'C', 'H1': 'C', 'O1': 'C'}\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "5951e44f9bc644fabf1c69e75b069379", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/1000000 [00:00